package com.sskj.wallet.presenter;

import com.lzy.okgo.model.Response;
import com.lzy.okrx2.adapter.FlowableBody;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.bean.CoinEnum;
import com.sskj.lib.bean.PageBean;
import com.sskj.lib.http.JsonCallBack;
import com.sskj.lib.http.JsonConverter;
import com.sskj.lib.util.ExceptionUtil;
import com.sskj.lib.util.WalletUtil;
import com.sskj.wallet.bean.BalanceBean;
import com.sskj.wallet.bean.BannerBean;
import com.sskj.wallet.bean.CoinAssetBean;
import com.sskj.wallet.bean.CoinPriceBean;
import com.sskj.wallet.bean.EthBalanceBean;
import com.sskj.wallet.bean.NoticeBean;
import com.sskj.wallet.http.HttpConfig;
import com.sskj.wallet.ui.fragment.WalletFragment;

import org.reactivestreams.Publisher;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;
import io.reactivex.Single;
import io.reactivex.SingleObserver;
import io.reactivex.SingleOperator;
import io.reactivex.SingleSource;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;


public class WalletFragmentPresenter extends BasePresenter<WalletFragment> {

    private String addr;

    public void setAddr(String addr) {
        this.addr = addr;
    }

    public void getBanner() {
        httpService.getBanner()
                .execute(new JsonCallBack<HttpData<NoticeBean>>() {
                    @Override
                    public void onSuccess(Response<HttpData<NoticeBean>> response) {
                        HttpData<NoticeBean> httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.updateMsg(httpData.getData().getUn_read());


                            mView.updateNotice(httpData.getData().getList());

                        }
                    }
                });
    }

    public void refresh() {
        showLoading();
        if (addr == null) {
            mView.refreshStop();
            return;
        }
//        if (WalletUtil.walletBean.getCode().equals(CoinEnum.ETH.getCode())) {
        httpService.getEthBalance(addr)
                .execute(new JsonCallBack<HttpData<EthBalanceBean>>() {
                    @Override
                    public void onSuccess(Response<HttpData<EthBalanceBean>> response) {
                        HttpData<EthBalanceBean> httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.OK) {
                            EthBalanceBean ethBalanceBeanHttpData = httpData.getData();

                            Flowable.fromIterable(ethBalanceBeanHttpData.getPrice())
                                    .map(coinPriceBean -> {
                                        CoinAssetBean coinAssetBean = new CoinAssetBean();
                                        coinAssetBean.setCode(coinPriceBean.getPname());
                                        coinAssetBean.setAddress(coinPriceBean.getContract_address());
                                        coinAssetBean.setPrice(coinPriceBean.getUsdt_price());
                                        coinAssetBean.setUsdt2yuan(coinPriceBean.getUsdt2yuan());
                                        return coinAssetBean;
                                    })
                                    .map(coinAssetBean -> {
                                        ArrayList<EthBalanceBean.TokensBean> tokensBeans = new ArrayList<>();
                                        if (WalletUtil.walletBean.getCode().equals(CoinEnum.ETH.getCode())) {
                                            EthBalanceBean.TokensBean tokensBean = new EthBalanceBean.TokensBean();
                                            tokensBean.setBalanceOf(ethBalanceBeanHttpData.getBalance());
                                            tokensBean.setCttAddr(WalletUtil.walletBean.getAddr());
                                            tokensBean.setCode(WalletUtil.walletBean.getCode());
                                            tokensBeans.add(tokensBean);
                                        }
                                        if (ethBalanceBeanHttpData.getTokens() != null) {
                                            tokensBeans.addAll(ethBalanceBeanHttpData.getTokens());
                                        }
                                        for (EthBalanceBean.TokensBean bean : tokensBeans) {
                                            String balanceOf = bean.getBalanceOf();
                                            String substring = balanceOf.substring(balanceOf.indexOf(" ") + 1);
                                            String money = balanceOf.substring(0, balanceOf.indexOf(" "));
                                            if (substring.equals(coinAssetBean.getCode()) || coinAssetBean.getCode().equals(bean.getCode())) {
                                                coinAssetBean.setAvailable(money);
//                                                    coinAssetBean.setAddress(tokensBean.getCttAddr());
                                            }
                                        }
                                        return coinAssetBean;
                                    })
                                    .map(coinAssetBean -> {
                                        String availbleCny = new BigDecimal(coinAssetBean.getAvailable()).multiply(new BigDecimal(coinAssetBean.getUsdt2yuan())).multiply(new BigDecimal(coinAssetBean.getPrice())).toPlainString();
                                        String priceCny = new BigDecimal(coinAssetBean.getPrice()).multiply(new BigDecimal(coinAssetBean.getUsdt2yuan())).toPlainString();
                                        coinAssetBean.setCnyPrice(priceCny);
                                        coinAssetBean.setCnyAvailable(availbleCny);
                                        return coinAssetBean;
                                    })
                                    .toList()
                                    .subscribe((coinAssetBeans, throwable) -> {
                                        BalanceBean balanceBean = new BalanceBean();
                                        Flowable.fromIterable(coinAssetBeans)
                                                .filter(coinAssetBean -> coinAssetBean.getCode().equals(WalletUtil.walletBean.getCode()))
                                                .subscribe(coinAssetBean -> {
                                                    balanceBean.setBalance(coinAssetBean.getCnyAvailable());
                                                    BigDecimal multiply = new BigDecimal(coinAssetBean.getAvailable()).multiply(new BigDecimal(coinAssetBean.getPrice()));
                                                    balanceBean.setUsdt(multiply.toPlainString());
                                                });
                                        balanceBean.setCoinAssetBeans(coinAssetBeans);
                                        mView.updateBalance(balanceBean);
                                    });
                        } else {
                            mView.refreshStop();
                        }
                    }
                });
//        } else {
//            BalanceBean balanceBean = new BalanceBean();
//            balanceBean.setBalance("0");
//            CoinAssetBean coinAssetBeanBtc = new CoinAssetBean();
//            CoinAssetBean coinAssetBeanUsdt = new CoinAssetBean();
//            coinAssetBeanUsdt.setCode(CoinEnum.USDT.getCode());
//            coinAssetBeanBtc.setCode(CoinEnum.BTC.getCode());
//            coinAssetBeanBtc.setAddress(WalletUtil.walletBean.getAddr());
//            ArrayList<CoinAssetBean> coinAssetBeans = new ArrayList<>();
//            coinAssetBeans.add(coinAssetBeanBtc);
//            coinAssetBeans.add(coinAssetBeanUsdt);
//            balanceBean.setCoinAssetBeans(coinAssetBeans);
//            mView.updateBalance(balanceBean);
//        }

    }
}
